package com.realestate.controller;

import java.util.Collection;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.realestate.constants.AppConstant;

@Controller
@RequestMapping("/admin")
public class InitController {
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView redirectToProduct(){
		Collection authen = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
		StringBuffer url = new StringBuffer();
		url.append("/");
		url.append(AppConstant.APP_URL);
		url.append("/admin/product/pages");
		return new ModelAndView(new RedirectView(url.toString()));
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login(@RequestParam(value = "error", required = false) String error,
			@RequestParam(value = "logout", required = false) String logout) {
		Collection authen = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
		ModelAndView model = new ModelAndView();
		if (error != null) {
			model.addObject("error", "Invalid username and password!");
		}

		if (logout != null) {
			model.addObject("msg", "You've been logged out successfully.");
		}
		model.setViewName("admin/login");

		return model;

	}
}
